//*******************************************************************
//  RollDicePanel.java       
//  Author: Fred Swartz
//  Location: http://www.leepoint.net/notes-java/examples/graphics/
//											rolldice/rolldice.html
//  Author: David J. Eck
//  Location: http://math.hws.edu/javanotes/c6/ex4-ans.html
//  Modified by: Jason M. Campbell
//  A panel in which to draw a pair of Dice.
//*******************************************************************

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RollDicePanel extends JPanel
{
	private Die leftDie;
	private Die rightDie;
		
	//private final int WIDTH = 125, HEIGHT = 95; // panel size
	private final int WIDTH = 122, HEIGHT = 88; // panel size
	
	private Timer timer; // timer used for dice animation
	private final int DELAY = 75;  // timer delay in milliseconds
	
	private boolean rollClicked;
	private JButton rollButton;
	
	private boolean rollDone;
	
	//---------------------------------------------------------------
	//  Constructor: Sets up this panel to listen for mouse events.
	//---------------------------------------------------------------
	public RollDicePanel()
	{
		leftDie = new Die();
		rightDie = new Die();
		rollButton = new JButton("ROLL");
		rollButton.setEnabled(false);
		DiceListener diceListener = new DiceListener();
		rollButton.addActionListener (diceListener);	
		
		rollClicked = false;
		rollDone = false;
		
		this.setLayout(new BorderLayout(0, 0));
		this.add(leftDie, BorderLayout.WEST);
		this.add(rightDie, BorderLayout.EAST);
		this.add(rollButton, BorderLayout.SOUTH);
		this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		
		setBackground (Color.black);
		setPreferredSize (new Dimension(WIDTH, HEIGHT));		
	} // end RollDicePanel constructor
	
	//***************************************************************
	//  Represents the listener for all mouse events.
	//***************************************************************
	private class DiceListener implements ActionListener
	{
		//-----------------------------------------------------------
		//  Captures the mouse button being pressed.
		//-----------------------------------------------------------
		public void actionPerformed(ActionEvent event)
		{
			roll();
			rollClicked = true;
			
		}
	} // end class DiceListener

	//***************************************************************
	//  Special roll function that incorporates a Timer object
	//  to display an "animated" dice roll.
	//***************************************************************
	
	public void roll()
	{
		if (timer != null)
			return;
		
		timer = new Timer(DELAY, new ActionListener() {
		int frames = 1;
		//-----------------------------------------------------------
		//  The actual rolling of the Die objects.
		//-----------------------------------------------------------
		public void actionPerformed(ActionEvent evt)
		{
			leftDie.roll();
			rightDie.roll();
		    repaint();
		    frames++;
		    if (frames == 10) {
		    	timer.stop();
		    	timer = null;
		    	rollDone = true;
		    }

		}
		});
		timer.start();
	} // end roll
	
	public int getDiceValue()
	{
		return leftDie.getFaceValue() + rightDie.getFaceValue();
	}
	
	public boolean rollButtonClicked()
	{
		return rollClicked;
	}
	
	public void resetRollButtonClicked()
	{
		rollClicked = false;
	}
	
	public void setRollButtonState(boolean state)
	{
		if(state == true)
			rollButton.setEnabled(true);
		else if(state == false)
			rollButton.setEnabled(false);
	}
	
	public boolean diceIsDoubles()
	{
		if(leftDie.getFaceValue() == rightDie.getFaceValue())
			return true;
		else
			return false;
	}
	
	public boolean rollIsDone()
	{
		if(rollDone == true)
		{
			rollDone = false;
			return true;
		}
		return false;
	}
	
} // end class RollDicePanel